package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Picture;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("PictureDAO")
@Transactional
public class PictureDAOImpl extends AbstractJpaDao<Picture> implements PictureDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Picture.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public PictureDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Picture findPictureById(Integer id) throws DataAccessException {

		return findPictureById(id, -1, -1);
	}

	@Transactional
	public Picture findPictureById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPictureById", startResult, maxRows, id);
			return (Picture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Picture> findPictureByName(String name) throws DataAccessException {

		return findPictureByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPictureByName", startResult, maxRows, name);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Set<Picture> findPictureByNameContaining(String name) throws DataAccessException {

		return findPictureByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPictureByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Set<Picture> findPictureByPath(String path) throws DataAccessException {

		return findPictureByPath(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByPath(String path, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findPictureByPath", startResult, maxRows, path);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Set<Picture> findPictureByPathContaining(String path) throws DataAccessException {

		return findPictureByPathContaining(path, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByPathContaining(String path, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPictureByPathContaining", startResult, maxRows, path);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Set<Picture> findPictureByDescription(String description) throws DataAccessException {

		return findPictureByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPictureByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Set<Picture> findPictureByDescriptionContaining(String description) throws DataAccessException {

		return findPictureByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findPictureByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@Transactional
	public Picture findPictureByPrimaryKey(Integer id) throws DataAccessException {
		return findPictureByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Picture findPictureByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPictureByPrimaryKey", startResult, maxRows, id);
			return (Picture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Picture> findPicturesByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findPicturesByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Picture> findAllPictures() throws DataAccessException {

		return findAllPictures(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findAllPictures(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllPictures", startResult, maxRows);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Picture> findPicturesByEvent(Integer eventId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllPicturesByEvent", start, max, eventId);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Picture> findPicturesByFood(Integer foodId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllPicturesByFood", start, max, foodId);
		return new LinkedHashSet<Picture>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Picture> findPictureByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Picture>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Picture> findPicturesByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Picture.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Picture> pictures = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return pictures;
	}

	@Transactional
	public Picture findPictureByPhysicalPrimaryKey(String name, String path) throws DataAccessException {
		return findPictureByPhysicalPrimaryKey(name, path, -1, -1);
	}

	@Transactional
	public Picture findPictureByPhysicalPrimaryKey(String name, String path, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findPictureByPhyscialPrimaryKey", startResult, maxRows, name, path);
			return (Picture) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Picture entity) {
		return true;
	}
}
